package com.ruoyi.product.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.product.domain.ProdAtta;
import com.ruoyi.product.domain.TbProdInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.product.domain.TbProd;
import com.ruoyi.product.service.ITbProdService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 商品信息Controller
 * 
 * @author ruoyi
 * @date 2025-08-01
 */
@RestController
@RequestMapping("/prod")
public class TbProdController extends BaseController
{
    @Autowired
    private ITbProdService tbProdService;

    /**
     * 查询商品信息列表
     */
    @RequiresPermissions("product:prod:list")
    @GetMapping("/list")
    public TableDataInfo list(TbProd tbProd)
    {
        startPage();
        List<TbProd> list = tbProdService.selectTbProdList(tbProd);
        return getDataTable(list);
    }

    /**
     * 数据从Es查询
     * @param tbProd
     * @return
     */
    @RequiresPermissions("product:prod:list")
    @GetMapping("/listFromEs")
    public TableDataInfo listFromEs(HttpServletRequest request,TbProd tbProd)
    {
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        TableDataInfo data=tbProdService.getListFromEs(tbProd,Integer.valueOf(pageNum),Integer.valueOf(pageSize));
        return data;
    }

    /**
     * 导出商品信息列表
     */
    @RequiresPermissions("product:prod:export")
    @Log(title = "商品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbProd tbProd)
    {
        List<TbProd> list = tbProdService.selectTbProdList(tbProd);
        ExcelUtil<TbProd> util = new ExcelUtil<TbProd>(TbProd.class);
        util.exportExcel(response, list, "商品信息数据");
    }

    /**
     * 获取商品信息详细信息
     */
    @RequiresPermissions("product:prod:query")
    @GetMapping(value = "/{prodId}")
    public AjaxResult getInfo(@PathVariable("prodId") Integer prodId)
    {
        return success(tbProdService.selectTbProdByProdId(prodId));
    }

    /**
     * 新增商品信息
     */
    @RequiresPermissions("product:prod:add")
    @Log(title = "商品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbProd tbProd)
    {
        return toAjax(tbProdService.insertTbProd(tbProd));
    }

    /**
     * 修改商品信息
     */
    @RequiresPermissions("product:prod:edit")
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbProd tbProd)
    {
        return toAjax(tbProdService.updateTbProd(tbProd));
    }

    /**
     * 删除商品信息
     */
    @RequiresPermissions("product:prod:remove")
    @Log(title = "商品信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{prodIds}")
    public AjaxResult remove(@PathVariable Integer[] prodIds)
    {
        return toAjax(tbProdService.deleteTbProdByProdIds(prodIds));
    }

    /**
     * 添加商品附件
     * @param prodAtta
     * @return
     */
    @PostMapping("/addAtta")
    public AjaxResult addAtta(@RequestBody ProdAtta prodAtta){
        prodAtta.setCreateTime(new Date());
        prodAtta.setCreateBy("system");
        return tbProdService.addAtta(prodAtta);
    }

    /**
     * 商品详情
     * @param tbProdInfo
     * @return
     */
    @GetMapping("/getProdInfoList/{prodId}")
    public TableDataInfo getProdInfoList(@PathVariable("prodId") Integer prodId){
        startPage();
         List<TbProdInfo> infoList =  tbProdService.getProdInfoList(prodId);
         return getDataTable(infoList);
    }
    /**
     * 获取商品详情
     * @param infoId
     * @return
     */
    @PostMapping("/getProdInfo/{infoId}")
    public AjaxResult getProdInfo(@PathVariable("infoId") Integer infoId){
        return tbProdService.getProdInfo(infoId);
    }

    /***
     *添加商品详情
     * @param tbProdInfo
     * @return
     */
    @PostMapping("/addProdInfo")
    public AjaxResult addProdInfo(@RequestBody TbProdInfo tbProdInfo){
        tbProdInfo.setCreateBy("Keven");
        tbProdInfo.setCreateTime(new Date());
        return tbProdService.addProdInfo(tbProdInfo);
    }

    /**
     * 创建索引
     * @return
     */
    @PostMapping("/createProdIndex")
    public AjaxResult createProdIndex(){
        return tbProdService.createProdIndex();
    }
    @PostMapping("/syncProdIndex")
    public  AjaxResult syncProdIndex(){
        return tbProdService.syncProdIndex();
    }
}
